package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.constants.ResultCode;
import com.syni.mdd.yhd.common.all.constants.ResultMsg;
import com.syni.mdd.yhd.common.all.dao.jpa.BmsDxFoodLibraryDao;
import com.syni.mdd.yhd.common.all.dto.requestDto.BmsDxFoodLibraryReDto;
import com.syni.mdd.yhd.common.all.dto.requestDto.BmsDxSubFoodReDto;
import com.syni.mdd.yhd.common.all.entity.BmsDxFoodLibrary;
import com.syni.mdd.yhd.common.all.entity.BmsDxSubFood;
import com.syni.mdd.yhd.common.all.service.*;
import com.syni.mdd.sjb.common.component.utils.common.utils.ResultMapHelper;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsDxFoodLibraryServiceImpl extends BaseService<BmsDxFoodLibrary, Integer> implements BmsDxFoodLibraryService {

    @Autowired
    BmsDxFoodLibraryDao bmsDxFoodLibraryDao;

    @Autowired
    BmsUserBusinessService bmsUserBusinessService;

    @Autowired
    BmsDxSubFoodServiceImpl bmsDxSubFoodService;



    @Override
    public BmsDxFoodLibraryDao getBaseDao() {
        return bmsDxFoodLibraryDao;
    }

    public Map<String, Object> getFoodLibraryType(BmsDxFoodLibraryReDto bmsDxFoodLibraryReDto) {
        Integer userId = Integer.parseInt(bmsDxFoodLibraryReDto.getUserId());
        Integer businessId = bmsDxFoodLibraryReDto.getBusinessId();
        Map<String,Object> checkMap = bmsUserBusinessService.checkBusinessIsHaving(userId,businessId);
        if(!ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString())){
            return checkMap;
        }
        Integer pageNum = bmsDxFoodLibraryReDto.getPageNum();
        Integer pageSize = bmsDxFoodLibraryReDto.getPageSize();
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.ASC,"newTime");
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsBusinessId",businessId);
        conditionMap.put("isDelete", Constants.COMMON_FLAG_FALSE_INT);
        List<BmsDxFoodLibrary> bmsDxFoodLibraryList = bmsDxFoodLibraryDao.findAll(new SpecificationUtils(conditionMap), pageable).getContent();
        return ResultMapHelper.success(bmsDxFoodLibraryList,ResultMsg.MSG_GET_SUCCESS);
    }

    @Override
    public Map<Integer,BmsDxFoodLibrary> getFoodLibraryByIds(Set<Integer> libraryIds,boolean isDisplayDelete) {
        Map<Integer,BmsDxFoodLibrary> bmsDxFoodLibraryMap = new HashMap<>();
        List<BmsDxFoodLibrary> bmsDxFoodLibraries;
        if(isDisplayDelete){
            bmsDxFoodLibraries = bmsDxFoodLibraryDao.findByIdIn(libraryIds);
        }else {
            bmsDxFoodLibraries = bmsDxFoodLibraryDao.findByIdInAndIsDelete(libraryIds,Constants.COMMON_FLAG_FALSE_INT);
        }
        if(bmsDxFoodLibraries != null && bmsDxFoodLibraries.size() > 0){
            for(BmsDxFoodLibrary bmsDxFoodLibrary : bmsDxFoodLibraries){
                bmsDxFoodLibraryMap.put(bmsDxFoodLibrary.getId(),bmsDxFoodLibrary);
            }
        }
        return bmsDxFoodLibraryMap;
    }

    public Map<String, Object> addFoodLibraryType(BmsDxFoodLibraryReDto bmsDxFoodLibraryReDto) {
        Integer userId = Integer.parseInt(bmsDxFoodLibraryReDto.getUserId());
        Integer businessId = bmsDxFoodLibraryReDto.getBusinessId();
        Map<String,Object> checkMap = bmsUserBusinessService.checkBusinessIsHaving(userId,businessId);
        if(!ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString())){
            return checkMap;
        }
        BmsDxFoodLibrary bmsDxFoodLibrary = new BmsDxFoodLibrary();
        bmsDxFoodLibrary.setBmsBusinessId(bmsDxFoodLibraryReDto.getBusinessId());
        if(bmsDxFoodLibraryReDto.getFoodTypeName().length() > 50){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_03,"类型名称过长");
        }
        bmsDxFoodLibrary.setFoodTypeName(bmsDxFoodLibraryReDto.getFoodTypeName());
        bmsDxFoodLibrary.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        bmsDxFoodLibraryDao.saveAndFlush(bmsDxFoodLibrary);
        return ResultMapHelper.success(bmsDxFoodLibrary, ResultMsg.HAND_SUCCESS);
    }

    @Transactional
    public Map<String, Object> deleteFoodLibraryType(BmsDxFoodLibraryReDto bmsDxFoodLibraryReDto) {
        Integer libraryId = bmsDxFoodLibraryReDto.getLibraryId();
        Optional<BmsDxFoodLibrary> bmsDxFoodLibrary = bmsDxFoodLibraryDao.findById(libraryId);
        if(!bmsDxFoodLibrary.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_FOOD_LIBRAY_NO_EXIST,"该菜品库类型不存在");
        }
        BmsDxFoodLibrary dxFoodLibrary = bmsDxFoodLibrary.get();
        dxFoodLibrary.setIsDelete(Constants.COMMON_FLAG_TRUE_INT);
        bmsDxFoodLibraryDao.save(dxFoodLibrary);
        //同时将菜品库类型下的所有菜品删除
        List<BmsDxSubFood> bmsDxSubFoods = bmsDxSubFoodService.getSubFoodByLibraryId(libraryId);
        if(bmsDxSubFoods != null && bmsDxSubFoods.size() > 0){
            for(BmsDxSubFood bmsDxSubFood : bmsDxSubFoods){
                bmsDxSubFood.setIsDelete(Constants.COMMON_FLAG_TRUE_INT);
            }
            bmsDxSubFoodService.getBaseDao().saveAll(bmsDxSubFoods);
        }
        return ResultMapHelper.success(dxFoodLibrary,ResultMsg.DELETE_SUCCESS);
    }

    public Map<String, Object> addDxSubFood(BmsDxSubFoodReDto bmsDxSubFoodReDto) {
        Integer userId = Integer.parseInt(bmsDxSubFoodReDto.getUserId());
        Integer businessId = bmsDxSubFoodReDto.getBusinessId();
        Map<String,Object> checkMap = bmsUserBusinessService.checkBusinessIsHaving(userId,businessId);
        if(!ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString())){
            return checkMap;
        }
        //再检验是否是该商家的菜品库
        Optional<BmsDxFoodLibrary> bmsDxFoodLibraryOp = bmsDxFoodLibraryDao.findById(bmsDxSubFoodReDto.getLibraryId());
        if(!bmsDxFoodLibraryOp.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_FOOD_LIBRAY_NO_EXIST,"菜品库类型不存在");
        }
        BmsDxFoodLibrary bmsDxFoodLibrary = bmsDxFoodLibraryOp.get();
        if(bmsDxFoodLibrary.getBmsBusinessId() != businessId.intValue()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_FOOD_LIBRAY_NO_BLONG,"菜品库类型不属于商家");
        }
        if(bmsDxSubFoodReDto.getFoodName().length() > 50){
            return ResultMapHelper.result(ResultCode.CODE_ERROR_03,"菜品名称过长");
        }
        BmsDxSubFood bmsDxSubFood = new BmsDxSubFood();
        bmsDxSubFood.setFoodName(bmsDxSubFoodReDto.getFoodName());
        bmsDxSubFood.setBmsBusinessId(bmsDxSubFoodReDto.getBusinessId());
        bmsDxSubFood.setBmsLibraryId(bmsDxSubFoodReDto.getLibraryId());
        bmsDxSubFood.setFoodPrice(Double.parseDouble(bmsDxSubFoodReDto.getFoodPrice()));
        bmsDxSubFood.setFoodName(bmsDxSubFoodReDto.getFoodName());
        bmsDxSubFood.setIsDelete(Constants.COMMON_FLAG_FALSE_INT);
        bmsDxSubFoodService.save(bmsDxSubFood);
        return ResultMapHelper.success(bmsDxSubFood,ResultMsg.INSERT_SUCCESS);
    }

    public Map<String, Object> getDxSubFood(BmsDxSubFoodReDto bmsDxSubFoodReDto) {
        Integer userId = Integer.parseInt(bmsDxSubFoodReDto.getUserId());
        Integer businessId = bmsDxSubFoodReDto.getBusinessId();
        Map<String,Object> checkMap = bmsUserBusinessService.checkBusinessIsHaving(userId,businessId);
        if(!ResultCode.CODE_NORMAL.equals(checkMap.get("status").toString())){
            return checkMap;
        }
        Integer pageNum = bmsDxSubFoodReDto.getPageNum();
        Integer pageSize = bmsDxSubFoodReDto.getPageSize();
        Integer libraryId = bmsDxSubFoodReDto.getLibraryId();
        Pageable pageable = BmsCommonService.getPageable(pageNum,pageSize, Sort.Direction.ASC,"newTime");
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("bmsLibraryId",libraryId);
        conditionMap.put("isDelete", Constants.COMMON_FLAG_FALSE_INT);
        List<BmsDxSubFood> bmsDxSubFoods = bmsDxSubFoodService.getBaseDao().findAll(new SpecificationUtils(conditionMap), pageable).getContent();
        return ResultMapHelper.success(bmsDxSubFoods,ResultMsg.MSG_GET_SUCCESS);
    }


    public Map<String, Object> deleteDxSubFood(BmsDxSubFoodReDto bmsDxSubFoodReDto) {
        Integer subFoodId = bmsDxSubFoodReDto.getSubFoodId();
        Optional<BmsDxSubFood> bmsDxSubFoodOptional = bmsDxSubFoodService.getBaseDao().findById(subFoodId);
        if(!bmsDxSubFoodOptional.isPresent()){
            return ResultMapHelper.result(ResultCode.CODE_BUSINESS_FOOD_LIBRAY_NO_EXIST,"菜品不存在");
        }
        BmsDxSubFood dxSubFood = bmsDxSubFoodOptional.get();
        dxSubFood.setIsDelete(Constants.COMMON_FLAG_TRUE_INT);
        bmsDxSubFoodService.getBaseDao().save(dxSubFood);
        return ResultMapHelper.success(dxSubFood,ResultMsg.DELETE_SUCCESS);
    }


    public Map<String, Object> getBusinessAllFood(BmsDxSubFoodReDto bmsDxSubFoodReDto) {
        Integer businessId = bmsDxSubFoodReDto.getBusinessId();
        //先找出所有菜品类型
        List<BmsDxFoodLibrary> bmsDxFoodLibraries = bmsDxFoodLibraryDao.findByBmsBusinessIdAndIsDelete(businessId, Constants.COMMON_FLAG_FALSE_INT);
        if (bmsDxFoodLibraries != null){
            Set<Integer> librayIds = bmsDxFoodLibraries.stream().map(BmsDxFoodLibrary::getId).collect(Collectors.toSet());
            Map<Integer,List<BmsDxSubFood>> foodListInLibrayMap = bmsDxSubFoodService.getSubFoodByType(librayIds);
            for(BmsDxFoodLibrary bmsDxFoodLibrary : bmsDxFoodLibraries){
                if(foodListInLibrayMap.containsKey(bmsDxFoodLibrary.getId())){
                    bmsDxFoodLibrary.setBmsDxSubFoods(foodListInLibrayMap.get(bmsDxFoodLibrary.getId()));
                }
            }
        }
        return ResultMapHelper.success(bmsDxFoodLibraries);
    }
}
